<?php include $this->admin_tpl('header');?>
<body>
<form class="layui-form layuimini-form" action="javascript:;" method="post" onsubmit="return dosub(this)">
    <div class="mainBox">
        <div class="main-container layui-container">
            <div class="layui-form-item">
                <label class="layui-form-label required">配置名称</label>
                <div class="layui-input-block">
                    <input type="text" id="name" name="name" value="<?php echo $data['name'];?>" lay-verify="required"
                           autocomplete="off" class="layui-input readonly-bg" lay-reqtext="配置名称不能为空"
                           readonly="readonly">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">配置类型</label>
                <div class="layui-input-inline">
                    <select name="fieldtype" class="select" style="width:230px" lay-filter="fieldtype" disabled>
                        <option value="text"
                        <?php if($data['fieldtype']=='text') echo "selected"; ?>>单行文本</option>
                        <option value="textarea"
                        <?php if($data['fieldtype']=='textarea') echo "selected"; ?>>多行文本</option>
                        <option value="image"
                        <?php if($data['fieldtype']=='image') echo "selected"; ?>>图片上传</option>
                        <option value="attachment"
                        <?php if($data['fieldtype']=='attachment') echo "selected"; ?>>附件上传</option>
                        <option value="radio"
                        <?php if($data['fieldtype']=='radio') echo "selected"; ?>>radio选项卡</option>
                        <option value="select"
                        <?php if($data['fieldtype']=='select') echo "selected"; ?>>option下拉框</option>
                        <option value="date"
                        <?php if($data['fieldtype']=='date') echo "selected"; ?>>日期</option>
                        <option value="datetime"
                        <?php if($data['fieldtype']=='datetime') echo "selected"; ?>>日期时间</option>
                    </select>
                    <input type="hidden" name="fieldtype" value="<?php echo $data['fieldtype']; ?>">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">配置值</label>

                <?php if($data['fieldtype']=='text') {?>
                <div class="layui-input-block upload_mode mode_text <?php if($data['fieldtype']!='text') echo 'layui-hide'; ?>">
                    <input type="text" class="layui-input" name="value[text]" autocomplete="off"
                           value="<?php echo $data['value'];?>">
                </div>
                <?php } ?>

                <?php if($data['fieldtype']=='textarea') {?>
                <div class="layui-input-block upload_mode mode_textarea <?php if($data['fieldtype']!='textarea') echo 'layui-hide'; ?>"
                     id="config_show">
                    <textarea name="value[textarea]" class="layui-textarea"
                              placeholder="例如：214243830"><?php echo $data['value'];?></textarea>
                </div>
                <?php } ?>

                <?php if($data['fieldtype']=='image') {?>
                <div class="layui-input-block upload_mode mode_image huicmf-upload <?php if($data['fieldtype']!='image') echo 'layui-hide'; ?>">
                    <input type="text" name="value[image]" value="<?php echo $data['value'];?>"
                           onmouseover="hui_img_preview('thumb-select-input',this.value)"
                           onmouseout="layer.closeAll();" id="thumb-select-input" autocomplete="off"
                           class="layui-input">
                    <button type="button" class="layui-btn layui-btn-normal layUpload" id="picker_pic2"
                            data-multiple="false" data-input-id="thumb-select-input"
                            data-preview-id="thumb" data-type="image"><i class="layui-icon"></i>上传图片
                    </button>
                    <a class="layui-btn"
                       data-open-pic="<?php echo U('admin/index/file_list',['type'=>'one','select_id'=>'thumb-select-input']) ?>">
                        <i class="layui-icon">&#xe67c;</i>选择图片
                    </a>
                </div>
                <?php } ?>

                <?php if($data['fieldtype']=='radio') {?>
                <div class="layui-input-block upload_mode mode_radio <?php if($data['fieldtype']!='radio') echo 'layui-hide'; ?>">
                    <?php echo $setting_data; ?>
                </div>
                <?php } ?>

                <?php if($data['fieldtype']=='select') {?>
                <div class="layui-input-block upload_mode mode_select <?php if($data['fieldtype']!='select') echo 'layui-hide'; ?>">
                    <select name="value[select]" class="select">
                        <?php echo $setting_data; ?>
                    </select>
                </div>
                <?php } ?>

                <?php if($data['fieldtype']=='date') {?>
                <div class="layui-input-block upload_mode mode_date <?php if($data['fieldtype']!='date') echo 'layui-hide'; ?>">
                    <input type="text" id="input-date" name="value[date]" autocomplete="off" value=""
                           class="layui-input" placeholder="yyyy-MM-dd">
                </div>
                <?php } ?>

                <?php if($data['fieldtype']=='datetime') {?>
                <div class="layui-input-block upload_mode mode_datetime <?php if($data['fieldtype']!='datetime') echo 'layui-hide'; ?>">
                    <input type="text" id="input-datetime" name="value[datetime]" autocomplete="off" value=""
                           class="layui-input" placeholder="yyyy-MM-dd HH:mm:ss">
                </div>
                <?php } ?>

            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">简洁说明</label>
                <div class="layui-input-block">
                    <input type="text" id="title" name="title" value="<?php echo $data['title'];?>" autocomplete="off"
                           class="layui-input" placeholder="例如：这个是站点QQ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否启用</label>
                <div class="layui-input-block" id="status">
                    <input type="radio" name="status" value="1"
                           title="启用" <?php if($data['status'] == 1) echo 'checked'; ?>>
                    <input type="radio" name="status" value="0"
                           title="禁用" <?php if($data['status'] == 0) echo 'checked'; ?>>
                </div>
            </div>
            <input type="hidden" name="id" value="<?php echo $data['id'];?>">

        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>

<?php include $this->admin_tpl('footer');?>
<script>
  layui.use(['layer', 'jquery', 'form', 'laydate'], function () {
    var layer = layui.layer,
      $ = layui.jquery,
      form = layui.form,
      laydate = layui.laydate;

    laydate.render({
      elem: '#input-date'
      , type: 'date'
    });

    laydate.render({
      elem: '#input-datetime'
      , type: 'datetime'
    });

    form.on('select(fieldtype)', function (data) {
      $('.upload_mode').hide();
      $('.mode_' + data.value).show();
    });
    //保存
    window.dosub = function (obj) {
      var reg = /^[a-zA-Z]{1}([a-zA-Z0-9]|[_]){0,19}$/;
      if (obj.name.value.length < 1 || obj.name.value.length > 20) {
        layer.msg('配置名称长度必须为1-20位！', {icon: 2});
        return false;
      }
      if ($("input[name='name']").val() == '' || !reg.test($("input[name='name']").val())) {
        layer.msg('配置名称必须为英文拼音数字_！', {icon: 2});
        return false;
      }
      $.ajax({
        type: 'POST',
        url: "<?php echo U('admin/config/custom_config_edit');?>",
        data: $(obj).serialize(),
        dataType: "json",
        success: function (res) {
          if (res.code === 200) {
            layer.msg(res.msg, {icon: 1, time: 2000}, function () {
              window.location.reload();
            })
          } else {
            layer.msg(res.msg, {icon: 2, time: 2000})
          }
        }
      });
    }
  });
</script>
</body>
</html>
